package com.shen.demo01;

public class SaleTicketDemo01 {
    public static void main(String[] args) {
        Ticket ticket=new Ticket();
        //多个线程抢夺同一个资源
        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                ticket.sale();
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                ticket.sale();
            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                ticket.sale();
            }
        },"C").start();

    }
}
///资源类
class Ticket{
    private int number =50;
    private int saleTicket=0;
    public synchronized void sale(){
        if(number<=50&&saleTicket<50){
            number=number-1;
            saleTicket=saleTicket+1;
            System.out.println(Thread.currentThread().getName()+"卖出了第"+saleTicket+"票，剩余:"+number+"票");
        }else{
            System.out.println("票已卖完！！");
        }
    }
}
